package org.apache.lucene.analysis.pinyin.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * pinyin4j工具类[pinyin4j并不能自动联系上下文判断多音字的正确拼音，
 * 如：重庆，不能返回chongqing,只能返回zhongqing和chongqing，让用户自己人工去选择正确拼音]
 */
public class Pinyin4jUtil {
    /**
     * 获取中文汉字拼音 默认输出
     *
     * @param chinese
     * @return
     */
    public static String getPinyin(String chinese) {
        return getPinyinZh_CN(makeStringByStringSet(chinese));
    }

    /**
     * 拼音大写输出
     *
     * @param chinese
     * @return
     */
    public static String getPinyinToUpperCase(String chinese) {
        return getPinyinZh_CN(makeStringByStringSet(chinese)).toUpperCase();
    }

    /**
     * 拼音小写输出
     *
     * @param chinese
     * @return
     */
    public static String getPinyinToLowerCase(String chinese) {
        return getPinyinZh_CN(makeStringByStringSet(chinese)).toLowerCase();
    }

    /**
     * 首字母大写输出
     *
     * @param chinese
     * @return
     */
    public static String getPinyinFirstToUpperCase(String chinese) {
        return getPinyin(chinese);
    }

    /**
     * 拼音简拼输出
     *
     * @param chinese
     * @return
     */
    public static String getPinyinJianPin(String chinese) {
        return getPinyinConvertJianPin(getPinyin(chinese));
    }

    /**
     * 拼音简拼输出[小写形式]
     *
     * @param chinese
     * @return
     */
    public static String getPinyinJianPinLowerCase(String chinese) {
        return getPinyinConvertJianPin(getPinyin(chinese)).toLowerCase();
    }

    /**
     * 字符集转换
     *
     * @param chinese
     * @return
     */
    public static Set<String> makeStringByStringSet(String chinese) {
        char[] chars = chinese.toCharArray();
        if (chinese != null && !chinese.trim().equalsIgnoreCase("")) {
            char[] srcChar = chinese.toCharArray();
            String[][] temp = new String[chinese.length()][];
            for (int i = 0; i < srcChar.length; i++) {
                char c = srcChar[i];

                // 是中文或者a-z或者A-Z转换拼音
                if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {

                    try {
                        temp[i] = PinyinHelper.toHanyuPinyinStringArray(
                            chars[i], getDefaultOutputFormat());

                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                    }
                } else if (((int) c >= 65 && (int) c <= 90)
                    || ((int) c >= 97 && (int) c <= 122)
                    || ((int) c >= 48 && (int) c <= 57)) {
                    //48-57是数字的ASC码十进制范围
                    temp[i] = new String[] {String.valueOf(srcChar[i])};
                } else {
                    temp[i] = new String[] {""};
                }
            }
            String[] pingyinArray = Exchange(temp);
            Set<String> zhongWenPinYin = new HashSet<String>();
            for (int i = 0; i < pingyinArray.length; i++) {
                zhongWenPinYin.add(pingyinArray[i]);
            }
            return zhongWenPinYin;
        }
        return null;
    }

    /**
     * Default Format 默认输出格式
     *
     * @return
     */
    public static HanyuPinyinOutputFormat getDefaultOutputFormat() {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 没有音调数字
        format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);// u显示
        return format;
    }

    /***************************************************************************
     *
     * @Name: Pinyin4jUtil.java
     * @Description: TODO
     * @author: wang_chian@foxmail.com
     * @version: Jan 13, 2012 9:39:54 AM
     * @param strJaggedArray
     * @return
     */
    public static String[] Exchange(String[][] strJaggedArray) {
        String[][] temp = doExchange(strJaggedArray);
        return temp[0];
    }

    private static String[][] doExchange(String[][] strJaggedArray) {
        int len = strJaggedArray.length;
        if (len >= 2) {
            int len1 = strJaggedArray[0].length;
            int len2 = strJaggedArray[1].length;
            int newlen = len1 * len2;
            String[] temp = new String[newlen];
            int Index = 0;
            for (int i = 0; i < len1; i++) {
                for (int j = 0; j < len2; j++) {
                    temp[Index] = capitalize(strJaggedArray[0][i])
                        + capitalize(strJaggedArray[1][j]);
                    Index++;
                }
            }
            String[][] newArray = new String[len - 1][];
            for (int i = 2; i < len; i++) {
                newArray[i - 1] = strJaggedArray[i];
            }
            newArray[0] = temp;
            return doExchange(newArray);
        } else {
            return strJaggedArray;
        }
    }

    /**
     * 首字母大写
     *
     * @param s
     * @return
     */
    public static String capitalize(String s) {
        char ch[];
        ch = s.toCharArray();
        if (ch != null && ch.length > 0) {
            if (ch[0] >= 'a' && ch[0] <= 'z') {
                ch[0] = (char) (ch[0] - 32);
            }
        }
        String newString = new String(ch);
        return newString;
    }

    /**
     * 字符串集合转换字符串(逗号分隔)
     *
     * @param stringSet
     * @return
     */
    public static String getPinyinZh_CN(Set<String> stringSet) {
        StringBuilder str = new StringBuilder();
        int i = 0;
        for (String s : stringSet) {
            if (i == stringSet.size() - 1) {
                str.append(s);
            } else {
                str.append(s + ",");
            }
            i++;
        }
        return str.toString();
    }

    /**
     * 获取每个拼音的简称
     *
     * @param chinese
     * @return
     */
    public static String getPinyinConvertJianPin(String chinese) {
        String[] strArray = chinese.split(",");
        String strChar = "";
        for (String str : strArray) {
            char arr[] = str.toCharArray(); // 将字符串转化成char型数组
            for (int i = 0; i < arr.length; i++) {
                if ((arr[i] >= 65 && arr[i] < 91) ||
                    (arr[i] >= 48 && arr[i] <= 57)) { // 判断是否是大写字母
                    strChar += arr[i] + "";
                }
            }
            strChar += ",";
        }
        return strChar;
    }

    public static HanyuPinyinOutputFormat getOutputFormat() {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        return format;
    }

    public static HanyuPinyinOutputFormat getOutputFormat(boolean upperCase) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(upperCase?HanyuPinyinCaseType.UPPERCASE:HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        return format;
    }

    public static HanyuPinyinOutputFormat getOutputFormat(boolean upperCase, int toneType, int vCharType) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(upperCase?HanyuPinyinCaseType.UPPERCASE:HanyuPinyinCaseType.LOWERCASE);
        if(toneType == 0) {
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        } else if(toneType == 1) {
            format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        } else if(toneType == 2) {
            format.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
        } else {
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        }

        if(vCharType == 0) {
            format.setVCharType(HanyuPinyinVCharType.WITH_V);
        } else if(vCharType == 1) {
            format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        } else if(vCharType == 2) {
            format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
        } else {
            format.setVCharType(HanyuPinyinVCharType.WITH_V);
        }

        return format;
    }

    public static Collection<String> getPinyinCollection(String chinese, boolean upperCase, int toneType, int vCharType) {
        ArrayList pinyinList = new ArrayList();
        HanyuPinyinOutputFormat defaultFormat = getOutputFormat(upperCase, toneType, vCharType);

        String[] array;
        try {
            for(int pinyins = 0; pinyins < chinese.length(); ++pinyins) {
                array = PinyinHelper.toHanyuPinyinStringArray(chinese.charAt(pinyins), defaultFormat);
                if(array != null && array.length > 0) {
                    pinyinList.add(array);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination var16) {
            var16.printStackTrace();
        }

        HashSet var17 = null;
        Iterator var8 = pinyinList.iterator();

        while(true) {
            while(var8.hasNext()) {
                array = (String[])var8.next();
                if(var17 != null && !var17.isEmpty()) {
                    HashSet var18 = var17;
                    var17 = new HashSet();
                    Iterator var20 = var18.iterator();

                    while(var20.hasNext()) {
                        String var19 = (String)var20.next();
                        String[] var15 = array;
                        int var14 = array.length;

                        for(int var13 = 0; var13 < var14; ++var13) {
                            String var21 = var15[var13];
                            var17.add(var19 + var21);
                        }
                    }
                } else {
                    var17 = new HashSet();
                    String[] charPinyin = array;
                    int var11 = array.length;

                    for(int pre = 0; pre < var11; ++pre) {
                        String pres = charPinyin[pre];
                        var17.add(pres);
                    }
                }
            }

            return var17;
        }
    }

    public static Collection<String> getPinyinCollection(String chinese, boolean upperCase, int toneType) {
        return getPinyinCollection(chinese, upperCase, toneType, 0);
    }

    public static Collection<String> getPinyinCollection(String chinese, boolean upperCase) {
        return getPinyinCollection(chinese, upperCase, 0, 0);
    }

    public static Collection<String> getPinyinCollection(String chinese) {
        return getPinyinCollection(chinese, false, 0, 0);
    }

    public static Collection<String> getPinyinShortCollection(String chinese, boolean upperCase, int toneType) {
        return getPinyinShortCollection(chinese, upperCase, toneType, 0);
    }

    public static Collection<String> getPinyinShortCollection(String chinese, boolean upperCase) {
        return getPinyinShortCollection(chinese, upperCase, 0, 0);
    }

    public static Collection<String> getPinyinShortCollection(String chinese) {
        return getPinyinShortCollection(chinese, false, 0, 0);
    }


    public static Collection<String> getPinyinShortCollection(String chinese, boolean upperCase, int toneType, int vCharType) {
        ArrayList pinyinList = new ArrayList();
        HanyuPinyinOutputFormat defaultFormat = getOutputFormat(upperCase, toneType, vCharType);

        try {
            for(int sorts = 0; sorts < chinese.length(); ++sorts) {
                String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(chinese.charAt(sorts), defaultFormat);
                if(pinyins != null && pinyins.length > 0) {
                    pinyinList.add(pinyins);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination var17) {
            var17.printStackTrace();
        }

        HashSet var18 = new HashSet();
        HashSet var19 = null;
        Iterator var9 = pinyinList.iterator();

        while(true) {
            while(var9.hasNext()) {
                String[] array = (String[])var9.next();
                if(var19 != null && !var19.isEmpty()) {
                    HashSet var20 = var19;
                    var19 = new HashSet();
                    Iterator var22 = var20.iterator();

                    while(var22.hasNext()) {
                        String var21 = (String)var22.next();
                        String[] var16 = array;
                        int var15 = array.length;

                        for(int var14 = 0; var14 < var15; ++var14) {
                            String var23 = var16[var14];
                            var19.add(var21 + var23.substring(0, 1));
                            var18.add(var21 + var23.substring(0, 1));
                        }
                    }
                } else {
                    var19 = new HashSet();
                    String[] charPinyin = array;
                    int var12 = array.length;

                    for(int pre = 0; pre < var12; ++pre) {
                        String pres = charPinyin[pre];
                        var19.add(pres.substring(0, 1));
                        var18.add(pres.substring(0, 1));
                    }
                }
            }

            return var18;
        }
    }


    public static void main(String[] args) {
        String str = "音乐 123 abc";
        System.out.println("小写输出：" + getPinyinToLowerCase(str));
        System.out.println("大写输出：" + getPinyinToUpperCase(str));
        System.out.println("首字母大写输出：" + getPinyinFirstToUpperCase(str));
        System.out.println("简拼大写输出：" + getPinyinJianPin(str));
        System.out.println("简拼小写输出：" + getPinyinJianPinLowerCase(str));
    }

    /**
     * HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
     　　         * UPPERCASE：大写  (ZHONG)
     　　         * LOWERCASE：小写  (zhong)
     　　        * format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
     　　        * WITHOUT_TONE：无音标  (zhong)
     　　        * WITH_TONE_NUMBER：1-4数字表示英标  (zhong4)
     　　        * WITH_TONE_MARK：直接用音标符（必须WITH_U_UNICODE否则异常）  (zhòng)
     　　        * format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
     　　        * WITH_V：用v表示ü  (nv)
     　　        * WITH_U_AND_COLON：用"u:"表示ü  (nu:)
     　　        * WITH_U_UNICODE：直接用ü (nü)
     　　        * format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
     */
}
